#!/bin/bash
file_name=functions.sh
funcs=`which $file_name`
if [ "$funcs" != "" ] ;then
  echo "funcs=$funcs"
  . $funcs
else
  funcs=`dirname $0`/$file_name
  echo "funcs=$funcs"
  if [ "$funcs" != "" ] ;then
    . $funcs
  else
    exit
  fi
fi

function setup_repo_xxx()
{
  local svn_default=/etc/apache2/sites-enabled/svn-default
  local select=$1
  cd /etc/apache2/sites-enabled/
  if [ -n "$select" ]; then
      echoTextBlue "Setting for $select ......"
      if [ "$select" = "home" ]; then
          sh_sed_file_r_row_include_xxx  $svn_default  80 "<VirtualHost 192.168.0.123:80>"
          sh_sed_file_r_row_include_xxx  $svn_default  ServerName      "ServerName 192.168.0.123"
          sudo sh -c 'sed -i "/\<svn\>/c\192.168.0.123  svn" /etc/hosts'
      elif [ "$select" = "comp" ]; then
	  sh_sed_file_r_row_include_xxx $svn_default   80 "<VirtualHost 172.20.120.123:80>"
          sh_sed_file_r_row_include_xxx $svn_default   ServerName      "ServerName 172.20.120.123"
          sudo sh -c 'sed -i "/\<svn\>/c\172.20.120.123  svn" /etc/hosts'
          sudo sh -c 'sed -i "/172.20.120.254/"d /etc/resolv.conf'
          sudo sh -c 'sed -i "/NetworkManager/a\\nameserver 172.20.120.254" /etc/resolv.conf'
      elif [ "$select" = "localhost" ]; then
          sh_sed_file_r_row_include_xxx $svn_default   80              "<VirtualHost localhost:80>"
          sh_sed_file_r_row_include_xxx $svn_default   ServerName      "ServerName   localhost"
          sudo sh -c 'sed -i "/\<svn\>/c\localhost  svn" /etc/hosts'
      fi
  else
    sh_sed_file_r_row_include_xxx $svn_default   80              "<VirtualHost localhost:80>"
    sh_sed_file_r_row_include_xxx $svn_default   ServerName      "ServerName   localhost"
    sudo sh -c 'sed -i "/\<svn\>/c\localhost  svn" /etc/hosts'
  fi
  sudo /etc/init.d/apache2 restart
  cd -
}

function setup_repo_auto()
{
if sh_at_home; then
  setup_repo_xxx home
else
  setup_repo_xxx comp
fi
}
function setup_repo_home()
{
  setup_repo_xxx home
}
function setup_repo_comp()
{
  setup_repo_xxx comp
}
function setup_repo_localhost()
{
  setup_repo_xxx localhost
}

function check_opt_status()
{
  local mount_point=/opt
  if `mount_point_is_busy $mount_point`; then
    echoTextRed "$mount_point is busy"
  else
    echoTextRed "/opt is not moounted!! return"
    mount.sh
  fi
}

check_opt_status
menu_list="
setup_repo_auto
setup_repo_home
setup_repo_comp
setup_repo_localhost"
result=`sh_menu_select "$menu_list"`
$result


